/**
 * Header.java
 * 2009-4-22 上午11:55:13
 *
 * 封装HTTP协议中的响应头部信息
 */
package com.esen.auwserv;

import java.util.HashMap;

/**
 * @author 何江舟
 *
 * 本类包含了HTTP响应头的一些信息，并提供一个默认构造器用来设置默认的HTTP响应头信息
 * 为了能根据用户请求生成不同的响应头信息，类提供类Setter方法来进行设置，并可Getter方法来获取想要的设置
 * 此外，类还可以像Apache和Tomcat一样，用XML配置文件代替
 */
public class ResponseHeader {

  public static final HashMap mimeType; // HashMap对象，保存一个“文件格式——MIME类型”对的列表，由于列表是固定的，所以使用final限定它

  private static final String contentEncoding = "UTF-8"; // 默认采用UTF-8编码

  private String method; // 客户端HTTP请求的方法，一般为GET，POST

  private String status; // 请求状态，包含HTTP版本号和状态信息，如“HTTP/1.1 200 OK”表示HTTP版本为1.1，状态代码为200，即找到了请求的资源

  private String mimeVersion; // MIME类型版本号，如MIME-version:1.0

  private String contentType; // 发送给客户端的内容类型，包含MIME类型和编码两部分，如text/html;charset=UTF-8

  private long contentLength; // 发送给客户端的内容长度

  static { // 使用静态初始化块初始MIME类型列表，由于这个列表可能在其它类中被直接应用，用静态初始化块可以在类被加载的时候就初始化，而不需要创建一个Header对象

    mimeType = new HashMap();

    // 把文件格式——MIME类型对添加到列表中，这里采用在代码中添加的方式
    // 其实这里使用XML配置文件来保存，然后通过程序从配置文件中读取的方式更合理，可以更方便的添加新的MIME类型
    // 这里为了降低程序的复杂度，就暂时采用这种方式，将来可以使用Java XML技术来修改这段代码
    mimeType.put("abs", "audio/x-mpeg");
    mimeType.put("ai", "application/postscript");
    mimeType.put("aif", "audio/x-aiff");
    mimeType.put("aiff", "audio/x-aiff");
    mimeType.put("aim", "application/x-aim");
    mimeType.put("art", "image/x-jg");
    mimeType.put("asf", "video/x-ms-asf");
    mimeType.put("au", "audio/basic");
    mimeType.put("avi", "video/x-rad-screenplay");
    mimeType.put("bcpio", "application/x-bcpio");
    mimeType.put("bin", "application/octet-stream");
    mimeType.put("bmp", "image/bmp");
    mimeType.put("body", "text/html;charset=" + contentEncoding);
    mimeType.put("cdf", "application/x-x509-ca-cert");
    mimeType.put("class", "application/java");
    mimeType.put("cpio", "application/x-cpio");
    mimeType.put("csh", "application/x-csh");
    mimeType.put("css", "text/css;charset=" + contentEncoding);
    mimeType.put("dib", "image/bmp");
    mimeType.put("doc", "application/msword");
    mimeType.put("dtd", "application/x-xml-dtd");
    mimeType.put("dv", "video/x-dv");
    mimeType.put("dvi", "application/x-dvi");
    mimeType.put("eps", "application/postscript");
    mimeType.put("etx", "text/x-setext;charset=" + contentEncoding);
    mimeType.put("exe", "application/octet-stream");
    mimeType.put("gif", "image/gif");
    mimeType.put("gtar", "application/x-gtar");
    mimeType.put("gz", "application/x-gzip");
    mimeType.put("hdf", "application/x-hdf");
    mimeType.put("hqx", "application/mac-binhex40");
    mimeType.put("htc", "text/x-component;charset=" + contentEncoding);
    mimeType.put("ai", "application/postscript");
    mimeType.put("htm", "text/html;charset=" + contentEncoding);
    mimeType.put("html", "text/html;charset=" + contentEncoding);
    mimeType.put("ico", "image/x-ico");
    mimeType.put("ief", "image/ief");
    mimeType.put("jad", "text/vnd.sun.j2me.app-descriptor;charset=" + contentEncoding);
    mimeType.put("jar", "application/java-archive");
    mimeType.put("java", "text/plain");
    mimeType.put("jnlp", "application/x-java-jnlp-file");
    mimeType.put("jpe", "image/jpeg");
    mimeType.put("jpg", "image/jpeg");
    mimeType.put("jpeg", "image/jpeg");
    mimeType.put("js", "text/javascript;" + contentEncoding);
    mimeType.put("jsf", "text/plain;charset=" + contentEncoding);
    mimeType.put("jspf", "text/plain;charset=" + contentEncoding);
    mimeType.put("kar", "audio/x-midi");
    mimeType.put("latex", "application/x-latex");
    mimeType.put("m3u", "audio/x-mpegurl");
    mimeType.put("mac", "image/x-macpaint");
    mimeType.put("man", "application/x-troff-man");
    mimeType.put("mathml", "application/mathml+xml");
    mimeType.put("me", "application/x-troff-me");
    mimeType.put("mid", "audio/x-midi");
    mimeType.put("midi", "audio/x-midi");
    mimeType.put("mif", "application/x-mif");
    mimeType.put("mov", "video/quicktime");
    mimeType.put("movie", "video/x-sgi-movie");
    mimeType.put("mp1", "audio/x-mpeg");
    mimeType.put("mp2", "audio/x-mpeg");
    mimeType.put("mp3", "audio/x-mpeg");
    mimeType.put("mp4", "audio/mp4");
    mimeType.put("mpa", "audio/x-mpeg");
    mimeType.put("mpe", "audio/mpeg");
    mimeType.put("mpeg", "audio/mpeg");
    mimeType.put("mpega", "audio/x-mpeg");
    mimeType.put("mpg", "audio/mpeg");
    mimeType.put("mpv2", "video/mpeg2");
    mimeType.put("ms", "application/x-wais-source");
    mimeType.put("nc", "application/x-netcdf");
    mimeType.put("oda", "application/oda");
    mimeType.put("odb", "application/vnd.oasis.opendocument.database");
    mimeType.put("odc", "application/vnd.oasis.opendocument.chart");
    mimeType.put("odf", "application/vnd.oasis.opendocument.formula");
    mimeType.put("odg", "application/vnd.oasis.opendocument.graphics");
    mimeType.put("odi", "application/vnd.oasis.opendocument.image");
    mimeType.put("odm", "application/vnd.oasis.opendocument.text-master");
    mimeType.put("odm", "application/vnd.oasis.opendocument.presentation");
    mimeType.put("ods", "application/vnd.oasis.opendocument.spreadsheet");
    mimeType.put("odt", "application/vnd.oasis.opendocument.text");
    mimeType.put("ogg", "application/ogg");
    mimeType.put("otg", "application/vnd.oasis.opendocument.graphics-template");
    mimeType.put("oth", "application/vnd.oasis.opendocument.text-web");
    mimeType.put("otp", "application/vnd.oasis.opendocument.presentatios-template");
    mimeType.put("ots", "application/vnd.oasis.opendocument.spreadsheet-template");
    mimeType.put("ott", "application/vnd.oasis.opendocument.text-template");
    mimeType.put("pbm", "image/x-portable-bitmap");
    mimeType.put("pct", "image/pict");
    mimeType.put("pdf", "application/pdf");
    mimeType.put("pgm", "image/x-portable-graymap");
    mimeType.put("pic", "image/pict");
    mimeType.put("pict", "image/pict");
    mimeType.put("pls", "audio/x-scpls");
    mimeType.put("png", "image/png");
    mimeType.put("pnm", "image/x-portable-anymap");
    mimeType.put("pnt", "image/x-macpaint");
    mimeType.put("ppm", "image/x-portable-pixmap");
    mimeType.put("ppt", "application/powerpoint");
    mimeType.put("ps", "application/postscript");
    mimeType.put("psd", "image/x-photoshop");
    mimeType.put("qt", "video/quicktime");
    mimeType.put("qti", "image/x-quicktime");
    mimeType.put("qtif", "image/x-quicktime");
    mimeType.put("ras", "image/x-cmu-raster");
    mimeType.put("rdf", "application/rdf+xml");
    mimeType.put("rgb", "image/x-rgb");
    mimeType.put("rm", "application/vnd.rn-realmedia");
    mimeType.put("roff", "application/x-troff");
    mimeType.put("rtf", "application/rtf");
    mimeType.put("rtx", "text/richtext");
    mimeType.put("sh", "application/x-sh");
    mimeType.put("shar", "application/x-shar");
    mimeType.put("xhtml", "application/xhtml+xml");
    mimeType.put("xml", "application/xml");
    mimeType.put("xsl", "application/xml");
    mimeType.put("xslt", "application/xslt+xml");
  }

  /**
   * 默认构造器，HTTP请求中没有相关信息时，采用默认头部信息，否则通过setter方法进行设置
   */
  public ResponseHeader() {
    method = "GET"; // 默认方式为GET
    status = "HTTP/1.1 200 OK"; // 默认HTTP版本1.1，默认状态200
    mimeVersion = "MIME-version:1.0"; // 默认MIME版本1.0
    contentType = "text/html;charset=UTF-8"; // 默认MIME类型为HTML文本，默认编码为UTF-8
    contentLength = 0L; // 默认长度为0
  }

  /**
   * 获得请求方法
   * 
   * @return 请求方法
   */
  public String getMethod() {
    return method;
  }

  /**
   * 设置请求方法
   * 
   * @param method 请求方法
   */
  public void setMethod(String method) {
    this.method = method;
  }

  /**
   * 获得请求状态
   * 
   * @return 请求状态
   */
  public String getStatus() {
    return status;
  }

  /**
   * 设置请求状态
   * 
   * @param status 请求状态
   */
  public void setStatus(String status) {
    this.status = status;
  }

  /**
   * 获得MIME版本
   * 
   * @return
   */
  public String getMimeVersion() {
    return mimeVersion;
  }

  /**
   * 设置MIME版本
   * 
   * @param mimeVersion
   */
  public void setMimeVersion(String mimeVersion) {
    this.mimeVersion = mimeVersion;
  }

  /**
   * 获得MIME类型
   * 
   * @return MIME类型
   */
  public String getContentType() {
    return contentType;
  }

  /**
   * 设置MIME类型
   * 
   * @param contentType
   */
  public void setContentType(String contentType) {
    this.contentType = contentType;
  }

  /**
   * 设置内容长度
   * 
   * @return 内容长度
   */
  public long getContentLength() {
    return contentLength;
  }

  /**
   * 设置内容长度
   * 
   * @param contentLength 内容长度
   */
  public void setContentLength(long contentLength) {
    this.contentLength = contentLength;
  }

  /**
   * 用HTTP协议的规范格式返回HTTP响应头部信息
   * 
   * @return HTTP响应头部信息
   */
  public String getHead() {
    return status + "\n" + mimeVersion + "\nContent-Type:" + contentType + "\nContent-Length:"
        + contentLength;
  }
}
